home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / docs / tidy / examples / .tmpurlgrab.php < prev   
PHP Script  |  2004-03-24  |  2KB  |  60 lines

  1. <?php
  2.  
  3.     /*
  4.      * urlgrab.php
  5.      *
  6.      * A simple command-line utility to extract all of the URLS contained
  7.      * within <A HREF> tags from a document.
  8.      *
  9.      * By: John Coggeshall <john@php.net>
  10.      *
  11.      * Usage: php urlgrab.php <file>
  12.      *
  13.      */
  14.          
  15.     /* Parse the document */
  16.     tidy_parse_file($_SERVER['argv'][1]);
  17.     
  18.     /* Fix up the document */
  19.     tidy_clean_repair();
  20.     
  21.     /* Get an object representing everything from the <HTML> tag in */
  22.     $html = tidy_get_html();
  23.     
  24.     /* Traverse the document tree */
  25.     print_r(get_links($html));
  26.     
  27.     function get_links($node) {
  28.         $urls = array();
  29.         
  30.         /* Check to see if we are on an <A> tag or not */
  31.         if($node->id == TIDY_TAG_A) {
  32.             /* If we are, find the HREF attribute */
  33.             $attrib = $node->get_attr(TIDY_ATTR_HREF);
  34.             if($attrib) {
  35.                 /* Add the value of the HREF attrib to $urls */
  36.                 $urls[] = $attrib->value;
  37.             }
  38.             
  39.         }
  40.         
  41.         /* Are there any children? */
  42.         if($node->has_children()) {
  43.             
  44.             /* Traverse down each child recursively */
  45.             foreach($node->children() as $child) {
  46.                    
  47.                 /* Append the results from recursion to $urls */
  48.                 foreach(get_links($child) as $url) {
  49.                     
  50.                     $urls[] = $url;
  51.                     
  52.                 }
  53.                 
  54.             }
  55.         }
  56.         
  57.         return $urls;
  58.     }
  59.     
  60. ?>